
// ReSharper disable CppClangTidyModernizeUseNodiscard
#pragma once
#include "Material.h"
#include "Texture2D.h"
#include "ShaderProgram.h"

class FDrawImgMaterial final
	: public FMaterial
{
public:

	enum ETextureType
	{
		Normal,
		Depth
	};

	explicit FDrawImgMaterial(const ETextureType InType = ETextureType::Normal) : Type(InType)
	{
	}

private:

	ETextureType Type;
	std::shared_ptr<FTexture2D> TextureBoard;


public:

	void SetTexture(const std::shared_ptr<FTexture2D>& Texture)
	{
		TextureBoard = Texture;
	}


	virtual bool Init() override
	{
		if (!FMaterial::Init())
		{
			return false;
		}

		Shader->SetLightingModel(ELightingModel::None);

		Shader->Use();

		Shader->SetInt("screenTexture", 0);

		return true;
	}

	virtual void Use() override
	{
		FMaterial::Use();

		TextureBoard->BindTextureUnit(0);
	}

	const std::string& GetFsSrcCode() const override
	{
		static std::string DepthCodeStr = R"(
			#version 410 core
			out vec4 FragColor;

			in vec2 TexCoords;
			
			uniform sampler2D screenTexture;
			//uniform float near_plane;
			//uniform float far_plane;

			void main()
			{
				float depthValue = texture(screenTexture, TexCoords).r;
			    FragColor = vec4(vec3(depthValue), 1.0);
			}
		)";

		static std::string NormalCodeStr = R"(
			#version 410 core
			out vec4 FragColor;

			in vec2 TexCoords;
			
			uniform sampler2D screenTexture;
			
			void main()
			{ 
			    FragColor = texture(screenTexture, TexCoords);
			}
		)";

		return Type == ETextureType::Depth ? DepthCodeStr : NormalCodeStr;
	}

	const std::string& GetVsSrcCode() const override
	{
		static std::string CodeStr = R"(

			#version 410 core 
			
			layout (location = 0) in vec3 aPos;
			layout (location = 2) in vec2 aTexCoords;
			
			out vec2 TexCoords;

			uniform mat4 model;

			void main()
			{
			    gl_Position = model * vec4(aPos, 1.0); 
			    TexCoords = aTexCoords;
			}

		)";

		return CodeStr;
	}
};

